<?php namespace system\middleware;

use houdunwang\middleware\build\Middleware;
use system\model\Config as ConfigModel;

class Config implements Middleware
{
    //执行中间件
    public function run($next)
    {
        //设置系统配置项
        $this->setSysConfig();
        //微信配置项
        $this->setWeiXinConfig();
        $next();
    }

    //设置微信配置项
    private function setWeiXinConfig()
    {
        $model = ConfigModel::find( 1 );
        $field = $model ? json_decode( $model[ 'weixin' ] , true ) : [];
//        \houdunwang\config\Config::set('wechat', $field);
//        Config::get('wechat.token');
//        提高优先级覆盖掉框架原来的wechat配置
        \Config::set('wechat',array_merge(\Config::get('wechat'),$field));
    }

    //设置系统配置项
    private function setSysConfig()
    {
        $model = ConfigModel::find(1);
        $field = $model ? json_decode($model['system'], true) : [];
        v('config', $field);
    }
}